package com.laborder.sysyy.controller.labs;


import com.laborder.sysyy.entity.*;
import com.laborder.sysyy.service.college.CollegeService;
import com.laborder.sysyy.service.labs.LabsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

 /**
   * @function:  分页查询
   * @author: 759057893@qq.com lyz
   * @date: 2019/1/13 13:13
   */
 @Controller
 @RequestMapping(value = "labs")
public class LabsController {
	@Autowired
	private LabsService labsService;
	 @Autowired
	 private CollegeService collegeService;

	 @GetMapping("toLabs")
	 public String toLabs(Model model){

		 List<College> collegeList = collegeService.findAllCollege();
		 model.addAttribute("collegeList",collegeList);
		 return "/labs/labsList";
	 }
	 /**
	   * @function: 实验室分页查询
	   * @author: 759057893@qq.com lyz
	   * @date: 2019/1/13 13:14
	   */
	 @ResponseBody
	@RequestMapping("findLabsWithPage")
	public Map<String,Object> findLabsWithPage(int page,int limit,String lName,String lRoom,String clName){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("page",(page-1)*limit);
		map.put("limit",limit);
		map.put("lName",lName);
		map.put("lRoom",lRoom);
		map.put("clName",clName);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code",0);
		resultMap.put("msg","");
		resultMap.put("count",labsService.getLabsCount(map));
		resultMap.put("data",labsService.findLabsWithPage(map));
		return  resultMap;
	}


	 @GetMapping("toAddLabs")
	 public String toAddLabs(Model model){
		 List<College> collegeList = collegeService.findAllCollege();
		 model.addAttribute("collegeList",collegeList);
		 return "/labs/labsAdd";
	 }
	 /**
	  * @function: 添加学生信息
	  * @author: 759057893@qq.com lyz
	  * @date: 2019/1/9 21:42
	  */
	 @ResponseBody
	 @RequestMapping("saveLabsInfo")
	 public  int saveLabsInfo(Labs lab) {
		 int flag = 0;
		 Labs s =labsService.getLabsInfoByName(lab.getlName(),lab.getlRoom());
		 if(s!=null){
			 flag = -1;
		 }else if(s==null){
			 flag = labsService.saveLabsInfo(lab);
		 }else{
			 flag = 0;
		 }
		 return flag;
	 }

	 /**
	  * @function: 删除实验室信息
	  * @author: 759057893@qq.com lyz
	  * @date: 2019/1/10 14:12
	  */
	 @ResponseBody
	 @RequestMapping("/delLabsInfo")
	 public  boolean delLabsInfo(int lId) {
		 boolean flag = false;
		 if(!flag){
			 flag = labsService.delLabsInfo(lId);
		 }
		 return flag;
	 }


	 /**
	  * @function: 批量删除实验室信息
	  * @author: 759057893@qq.com lyz
	  * @date: 2019/1/10 14:12
	  */
	 @ResponseBody
	 @RequestMapping("/delManyLabsInfo")
	 public  boolean delManyLabsInfo( @RequestParam("lId[]") int lId[]  ) {
		 boolean flag = false;
		 for(Integer lIds: lId){
			 flag  = labsService.delManyLabsInfo(lIds);
		 }
		 return flag;
	 }

	 /**
	  * @function:  实验室信息查看
	  * @author: 759057893@qq.com lyz
	  * @date: 2019/1/10 14:45
	  */
	 @RequestMapping("labsDetail")
	 public String labsDetail(Model model,String lId) {
		 Labs labs = labsService.getLabsInfo(lId);
		 model.addAttribute("labs", labs);
		 return "labs/labsDetail";
	 }

	 /**
	  * @function:  实验室信息修改前回显信息
	  * @author: 759057893@qq.com lyz
	  * @date: 2019/1/10 14:45
	  */
	 @RequestMapping("editLabs")
	 public  String editLabs(Model model,String lId){
		 Labs labs = labsService.getLabsInfo(lId);
		 model.addAttribute("labs", labs);
		 List<College> collegeList = collegeService.findAllCollege();
		 model.addAttribute("collegeList",collegeList);
		 return "labs/editLabs";
	 }

	 /**
	  * @function: 修改实验室数据
	  * @author: 759057893@qq.com lyz
	  * @date: 2019/1/10 16:00
	  */
	 @ResponseBody
	 @RequestMapping("/updateLabsInfo")
	 public  boolean updateLabsInfo(Labs labs) {

		 return labsService.updateLabsInfo(labs);
	 }
}